package com.kkb.service.impl;


import com.kkb.mapper.TbGoodsMapper;
import com.kkb.mapper.TbImageMapper;
import com.kkb.mapper.TbProductInfoMapper;
import com.kkb.mapper.TbProductMapper;
import com.kkb.pojo.*;
import com.kkb.service.TbProductService;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Transactional
@Service
public class TbProductServiceImpl implements TbProductService {

    @Autowired
    private TbProductMapper tbProductMapper;

    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    @Autowired
    private TbProductInfoMapper tbProductInfoMapper;

    @Autowired
    private TbImageMapper tbImageMapper;

    @Override
    public List<TbProduct> getAll() {
        TbProductExample example = new TbProductExample();
        List<TbProduct> tbProducts = tbProductMapper.selectByExample(example);
        return tbProducts;
    }

    @Override
    @Transactional()
    public int addTbProduct(TbProduct tbProduct) {
        //根据class3查询class2和class1
        String extrAttribute=tbProduct.getBak1();
        tbProduct.setCreateTime(new Date());
        tbProduct.setStatus("1");
        tbProduct.setIsDelete("N");
        tbProduct.setBak1("");
        int result=tbProductMapper.insert(tbProduct);
        //存入成功之后查询刚存入的productId，关联productInfo表存入扩展属性
        TbProductExample example = new TbProductExample();
        TbProductExample.Criteria criteria = example.createCriteria();
        criteria.andProductNameEqualTo(tbProduct.getProductName());
        Integer productId = tbProductMapper.selectByExample(example).get(0).getProductId();

        //遍历goods存入goods表
        List<TbGoods> goodsList = tbProduct.getGoodsList();
        if(goodsList!=null){
            for (TbGoods tbGoods : goodsList) {
                String imgUrl=tbGoods.getBak1();
                tbGoods.setProductId(productId);
                tbGoods.setCreateTime(new Date());
                tbGoods.setIsDelete("N");
                tbGoods.setBak1(null);
                tbGoodsMapper.insert(tbGoods);
                if(imgUrl!=null){
                    //goods表存入成功后，查询刚存入的goodsId，关联表存入tb_img存入图片路径
                    TbGoodsExample tbGoodsExample = new TbGoodsExample();
                    TbGoodsExample.Criteria criteria1 = tbGoodsExample.createCriteria();
                    criteria1.andGoodsNameEqualTo(tbGoods.getGoodsName());
                    Integer goodsId = tbGoodsMapper.selectByExample(tbGoodsExample).get(0).getGoodsId();
                    //根据goodsId，关联表存入tb_img存入图片路径
                    TbImage tbImage = new TbImage();
                    tbImage.setGoodsId(goodsId);
                    //获取颜色
                    tbImage.setImgPath(imgUrl);
                    tbImage.setCreateTime(new Date());
                    tbImage.setIsDelete("N");
                    tbImageMapper.insert(tbImage);
                }

            }
        }

        TbProductInfoWithBLOBs tbProductInfo = new TbProductInfoWithBLOBs();
        tbProductInfo.setExtrattribute(extrAttribute);
        tbProductInfo.setProductId(productId);
        tbProductInfo.setCreateTime(new Date());
        tbProductInfo.setIsDelete("N");
        tbProductInfoMapper.insert(tbProductInfo);
        return result;
    }

    @Override
    public List<TbProduct> getlist() {
        TbProductExample example = new TbProductExample();
        List<TbProduct> tbProducts = tbProductMapper.selectByExample(example);
        return tbProducts;
    }
}
